package com.pro.controller;

import com.pro.common.GuavaCommon;
import com.pro.pojo.JsonData;
import com.pro.pojo.SyDictData;
import com.pro.service.SyDictDataService;
import com.pro.utils.GuavaUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
//字典数据 控制层
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.concurrent.ExecutionException;

@RequestMapping(value = "/SyDictData")
@Controller
public class SyDictDataController {
    @Autowired
    private SyDictDataService syDictDataService;

    @Autowired
    private GuavaUtils guavaUtils;

    //根据字典类型查询字典数据
    /*@RequestMapping(value = "/getSyDictDatas")
    @ResponseBody
    public JsonData getSyDictDatas(String syDictType)  {
        Object ifPresent = guavaUtils.getTenCacheBuild().getIfPresent(String.valueOf(GuavaCommon.Navi));
        if(ifPresent != null){
            return ifPresent != null ? JsonData.buildSuccess(ifPresent) : JsonData.buildError("服务器出错了");
        }
        List<SyDictData> syDictData = getSyDictData(syDictType);
        guavaUtils.getTenCacheBuild().put(String.valueOf(GuavaCommon.Navi),syDictData);
        return syDictData != null ? JsonData.buildSuccess(syDictData) : JsonData.buildError("服务器出错了");
    }

    private List<SyDictData> getSyDictData(String syDictType){
        List<SyDictData> oneLevelNavs = syDictDataService.getSyDictDatasBySyDictType(syDictType, 0);
        for (SyDictData oneLevelNav : oneLevelNavs) {
            //获取二级目录
            oneLevelNav.setChildren(syDictDataService.getSyDictDatasBySyDictType(syDictType,oneLevelNav.getDictCode()));
            List<SyDictData> twoLevelNavs = oneLevelNav.getChildren();
            for (SyDictData twoLevelNav : twoLevelNavs) {
                //获取3级目录
                twoLevelNav.setChildren(syDictDataService.getSyDictDatasBySyDictType(syDictType,twoLevelNav.getDictCode()));
            }
        }
        return oneLevelNavs;
    }*/


    //查询操作日志
    @RequestMapping("/getExamineJournalTable")
    @ResponseBody
    public JsonData getExamineJournal(){
        List<SyDictData> list = syDictDataService.getExamineJournal();
        return list!=null?JsonData.buildSuccess(list):JsonData.buildError("服务器出错了");
    }

    //根据条件查询操作日志
    @RequestMapping("/getTableByCondition")
    @ResponseBody
    public JsonData getExamineJournalByCondition(String createBy,String cssClass,String dictValue, String createTime){
        List<SyDictData> list = syDictDataService.getExamineJournalByCondition(createBy,cssClass,dictValue,createTime);
        return list!=null?JsonData.buildSuccess(list):JsonData.buildError("服务器出错了");
    }

    //删除操作日志
    @RequestMapping("/deleteTableByCode")
    @ResponseBody
    public JsonData deleteExamineJournalByCode(int dictCode){
        boolean b = syDictDataService.deleteExamineJournalByCode(dictCode);
        return b?JsonData.buildSuccess(b):JsonData.buildError("服务器出错了");
    }

    @RequestMapping("/addProductJournal")
    @ResponseBody
    public JsonData addProductJournal(SyDictData syDictData){
        boolean b = syDictDataService.addProductJournal(syDictData);
        return b?JsonData.buildSuccess(b):JsonData.buildError("服务器出错了");
    }

    @RequestMapping("/addProductPrice")
    @ResponseBody
    public JsonData addProductPrice(SyDictData syDictData){
        boolean b = syDictDataService.addProductPrice(syDictData);
        return b?JsonData.buildSuccess(b):JsonData.buildError("服务器出错了");
    }

    @RequestMapping("/getProductPrice")
    @ResponseBody
    public JsonData getProductPrice(String dictLabel, String dictValue, String createTime){
        List<SyDictData> productPrice = syDictDataService.getProductPrice(dictLabel,dictValue,createTime);
        return productPrice!=null?JsonData.buildSuccess(productPrice):JsonData.buildError("服务器出错了");
    }

}
